import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { PoetryCollection, Poetry, LearningProgress } from '@/types'

export const usePoetryStore = defineStore('poetry', () => {
  const poetryCollection = ref<PoetryCollection | null>(null)
  const isLoading = ref(false)
  
  // 加载古诗数据
  const loadPoetryData = async () => {
    isLoading.value = true
    try {
      const response = await fetch('/data/poetry_collection.json')
      poetryCollection.value = await response.json()
    } catch (error) {
      console.error('Failed to load poetry data:', error)
    } finally {
      isLoading.value = false
    }
  }
  
  // 根据ID获取古诗
  const getPoetryById = (id: number) => {
    return poetryCollection.value?.poems.find(p => p.id === id)
  }
  
  // 按主题分类
  const getPoemsByTheme = (theme: string) => {
    if (!poetryCollection.value) return []
    // 根据主题关键词匹配
    const themeKeywords: Record<string, string[]> = {
      'spring': ['春', '柳', '花'],
      'summer': ['夏', '荷', '池'],
      'autumn': ['秋', '霜', '枫'],
      'winter': ['雪', '寒', '江'],
      'animal': ['鹅', '鸟', '鹭'],
      'landscape': ['山', '水', '瀑'],
      'family': ['母', '子', '游']
    }
    
    const keywords = themeKeywords[theme] || []
    return poetryCollection.value.poems.filter(poem => 
      keywords.some(keyword => 
        poem.title.includes(keyword) || 
        poem.original_text.includes(keyword)
      )
    )
  }
  
  // 按作者分类
  const getPoemsByAuthor = (author: string) => {
    return poetryCollection.value?.poems.filter(p => p.author === author) || []
  }
  
  // 按朝代分类
  const getPoemsByDynasty = (dynasty: string) => {
    return poetryCollection.value?.poems.filter(p => p.dynasty === dynasty) || []
  }
  
  // 按难度分类
  const getPoemsByDifficulty = (difficulty: string) => {
    if (!poetryCollection.value) return []
    
    return poetryCollection.value.poems.filter(poem => {
      const wordCount = poem.original_text.length
      switch (difficulty) {
        case 'beginner': return wordCount <= 20
        case 'intermediate': return wordCount > 20 && wordCount <= 28
        case 'advanced': return wordCount > 28
        default: return true
      }
    })
  }
  
  // 获取所有作者
  const getAllAuthors = computed(() => {
    if (!poetryCollection.value) return []
    return [...new Set(poetryCollection.value.poems.map(p => p.author))]
  })
  
  // 获取所有朝代
  const getAllDynasties = computed(() => {
    if (!poetryCollection.value) return []
    return [...new Set(poetryCollection.value.poems.map(p => p.dynasty))]
  })
  
  // 搜索古诗
  const searchPoems = (query: string) => {
    if (!poetryCollection.value || !query.trim()) return []
    
    const searchTerm = query.toLowerCase()
    return poetryCollection.value.poems.filter(poem =>
      poem.title.toLowerCase().includes(searchTerm) ||
      poem.author.toLowerCase().includes(searchTerm) ||
      poem.original_text.toLowerCase().includes(searchTerm)
    )
  }
  
  return {
    poetryCollection,
    isLoading,
    loadPoetryData,
    getPoetryById,
    getPoemsByTheme,
    getPoemsByAuthor,
    getPoemsByDynasty,
    getPoemsByDifficulty,
    getAllAuthors,
    getAllDynasties,
    searchPoems
  }
})